package com.atguigu.gmall.config;

import com.atguigu.gmall.constant.MqConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class CancelOrderQueueConfig {

        //定义取消订单的队列
        @Bean
        public Queue cancelOrderQueue(){
            return new Queue(MqConst.CANCEL_ORDER_QUEUE,false);
        }
        //定义取消订单的交换机
        @Bean
        public CustomExchange cancelOrderExchange(){
            Map<String, Object> args = new HashMap<>();
            //自定义交换机的类型
            args.put("x-delayed-type", "direct");
            return new CustomExchange(MqConst.CANCEL_ORDER_EXCHANGE,"x-delayed-message",false,true,args);
        }
        //定义绑定关系
        @Bean
        public Binding bindingDelayedQueue(@Qualifier("cancelOrderQueue") Queue cancelOrderQueue,
                                           @Qualifier("cancelOrderExchange") CustomExchange cancelOrderExchange) {
            return BindingBuilder.bind(cancelOrderQueue).to(cancelOrderExchange).with(MqConst.CANCEL_ORDER_ROUTE_KEY).noargs();
        }
    /*@Bean
    public Queue cancelOrderQueue(){
        return new Queue(MqConst.CANCEL_ORDER_QUEUE,false);
    }
    //由于采用延迟插件 自定义交换机
    @Bean
    public CustomExchange cancelOrderExchange(){
        Map<String, Object> arguments=new HashMap<>();
        arguments.put("x-delayed-type","direct"); //常规交换机类型
        return new CustomExchange(MqConst.CANCEL_ORDER_EXCHANGE,"x-delayed-message",false,true,arguments);
    }
    //队列和交换机的绑定
    @Bean
    public Binding bindingDelayedQueue(@Qualifier("cancelOrderQueue") Queue cancelOrderQueue,
                                       @Qualifier("cancelOrderExchange") CustomExchange cancelOrderExchange){
        return BindingBuilder.bind(cancelOrderQueue).to(cancelOrderExchange).with(MqConst.CANCEL_ORDER_ROUTE_KEY).noargs();
    }*/
}
